package com.bw.ppjokeplay.mvvm.view

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.bw.ppjokeplay.BR
import com.bw.ppjokeplay.R
import com.bw.ppjokeplay.commen.mvvm.view.BaseActivity
import com.bw.ppjokeplay.commen.widget.BottomButton
import com.bw.ppjokeplay.databinding.TestActivityBinding
import com.bw.ppjokeplay.mvvm.view.find.FindFragment
import com.bw.ppjokeplay.mvvm.view.home.HomeFragment
import com.bw.ppjokeplay.mvvm.view.my.MyFragment
import com.bw.ppjokeplay.mvvm.view.sofa.SofaFragment
import com.bw.ppjokeplay.mvvm.viewmodel.TestViewModel

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/9 0009 下午 7:35
 *
 */
class TestActivity: BaseActivity<TestActivityBinding, TestViewModel>(),BottomButton.BottomSelectListenner {

    private var frameLiner: LinearLayout? = null
    private var Home: BottomButton? = null
    private var Category: BottomButton? = null
    private var ShopCar: BottomButton? = null
    private var Mine: BottomButton? = null
    private var mainFgGroup: FrameLayout? = null
    private var homeFragment: Fragment? = null
    private var sofaFragment: Fragment? = null
    private var findFragment: Fragment? = null
    private var myFragment: Fragment? = null
    private var xx: BottomButton? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        Home!!.setContact("首页")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_home)
            .setSelImg(R.drawable.home1)
            .setNamesize(6)
            .setSelsize(7)
            .setListenner(this)
            .refresh()
        Category!!.setContact("休闲")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_sofa)
            .setSelImg(R.drawable.sofa1)
            .setNamesize(6)
            .setSelsize(7)
            .setListenner(this)
            .refresh()
        ShopCar!!.setContact("")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.add)
            .setSelImg(R.drawable.add)
            .setNamesize(6)
            .setSelsize(8)
            .setListenner(this)
            .refresh()
        xx!!.setContact("视频")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_movie)
            .setSelImg(R.drawable.movie1)
            .setNamesize(6)
            .setSelsize(7)
            .setListenner(this)
            .refresh()
        Mine!!.setContact("我的")
            .setNomalColor(Color.BLACK)
            .setSelColor(Color.RED)
            .setNomalImg(R.drawable.icon_tab_mine)
            .setSelImg(R.drawable.mine1)
            .setNamesize(6)
            .setSelsize(7)
            .setListenner(this)
            .refresh()
        onSelect(R.id.Home)
    }
    override fun bindLayout(): Int {
         return R.layout.test_activity
    }

    override fun initVerable(): Int {
        return BR.test
    }

    override fun onSelect(id: Int) {
        clearBottomsel()
        hideFagment()
        val manager = supportFragmentManager
        val ft = manager.beginTransaction()
        when (id) {
            R.id.Home -> {
                Home!!.setSetFlag(true)
                if (homeFragment == null) {
                    homeFragment = HomeFragment()
                    ft.add(R.id.main_fg_group, homeFragment as HomeFragment)
                } else {
                    ft.show(homeFragment!!)
                }
            }
            R.id.Category -> {
                Category!!.setSetFlag(true)
                if (sofaFragment == null) {
                    sofaFragment = SofaFragment()
                    ft.add(R.id.main_fg_group, sofaFragment as SofaFragment)
                } else {
                    ft.show(sofaFragment!!)
                }
            }
            R.id.xx -> {
                xx!!.setSetFlag(true)
                if (findFragment == null) {
                    findFragment = FindFragment()
                    ft.add(R.id.main_fg_group, findFragment as FindFragment)
                } else {
                    ft.show(findFragment!!)
                }
            }
            R.id.Mine -> {
                Mine!!.setSetFlag(true)
                if (myFragment == null) {
                    myFragment = MyFragment()
                    ft.add(R.id.main_fg_group, myFragment as MyFragment)
                } else {
                    ft.show(myFragment!!)
                }
            }
            R.id.ShopCar -> {
                startActivity(Intent(this,LiveActivity::class.java))
            }
        }
        ft.commit()

    }
    private fun hideFagment() {
        val manager = supportFragmentManager
        val ft = manager.beginTransaction()
        if (homeFragment != null) ft.hide(homeFragment!!)
        if (sofaFragment != null) ft.hide(sofaFragment!!)
        if (findFragment != null) ft.hide(findFragment!!)
        if (myFragment != null) ft.hide(myFragment!!)
//        if (shopfragment != null) ft.hide(shopfragment)
        ft.commit()
    }

    private fun clearBottomsel() {
        Home!!.setSetFlag(false)
        Category!!.setSetFlag(false)
//        ShopCar!!.setSetFlag(false)
        Mine!!.setSetFlag(false)
        xx!!.setSetFlag(false)
    }
    private fun initView() {
        frameLiner = findViewById(R.id.frame_liner) as LinearLayout
        Home = findViewById(R.id.Home) as BottomButton
        Category = findViewById(R.id.Category) as BottomButton
        ShopCar = findViewById(R.id.ShopCar) as BottomButton
        Mine = findViewById(R.id.Mine) as BottomButton
        xx = findViewById(R.id.xx) as BottomButton
        mainFgGroup = findViewById(R.id.main_fg_group) as FrameLayout
    }
}